package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.BathHeater;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 浴霸数据库操作
 */

public class BathHeaterDao {
    private Context mContext;
    private Dao<BathHeater, Integer> bathHeaterDaoOpe;
    private DatabaseHelper helper;

    public BathHeaterDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            bathHeaterDaoOpe = helper.getDao(BathHeater.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(BathHeater bathHeater) {
        try {
            bathHeater.setChecked(false);
            bathHeaterDaoOpe.create(bathHeater);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateBathHeater(BathHeater bathHeater) {
        try {
            bathHeater.setChecked(false);
            return bathHeaterDaoOpe.update(bathHeater);
            //    LogHelper.i("updateLightBathHeaternumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public BathHeater get(int id) {
        try {
            return bathHeaterDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public BathHeater getByUuid(String uuid) {
        List<BathHeater> bathHeaterList = null;
        try {
            bathHeaterList = bathHeaterDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (bathHeaterList != null && bathHeaterList.size() > 0) {
                return bathHeaterList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * @return
     */
    public List<BathHeater> getAllBathHeater() {
        List<BathHeater> bathHeaterList = new ArrayList<>();
        try {
            bathHeaterList = bathHeaterDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bathHeaterList;
    }

    public BathHeater getById(String groupId,int category) {
        List<BathHeater> bathHeaterList = null;
        try {
            bathHeaterList = bathHeaterDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (bathHeaterList != null && bathHeaterList.size() > 0) {
                return bathHeaterList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<BathHeater> getFamilyBathHeaters(String familyId,int category) {
        List<BathHeater> bathHeaterList = new ArrayList<>();
        try {
            bathHeaterList = bathHeaterDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bathHeaterList;
    }

    public int delete(BathHeater bathHeater) {
        try {
            return bathHeaterDaoOpe.delete(bathHeater);
            //    LogHelper.i("deleLightBathHeaternumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = bathHeaterDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<BathHeater> bathHeaterList) {
        try {
            int number = bathHeaterDaoOpe.delete(bathHeaterList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyBathHeaters(String familyId) {
        try {
            List<BathHeater> bathHeaterList = getFamilyBathHeaters(familyId,5);
            int number = bathHeaterDaoOpe.delete(bathHeaterList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public BathHeater getBathHeaterByName(String familyId, String name,int category) {
        try {
            List<BathHeater> bathHeaterList = bathHeaterDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (bathHeaterList != null && bathHeaterList.size() > 0)
                return bathHeaterList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
